using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AttendantsDSTableAdapters;
using NursesDSTableAdapters;
using DoctorsDSTableAdapters;
using System.Diagnostics;
using System.Collections.Generic;

public partial class Login : System.Web.UI.Page
{
    #region Handlers

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ctrlMainLogin_LoggedIn(object sender, EventArgs e)
    {
        CheckAndRedirect();
    }

    #endregion

    #region Methods

    private void CheckAndRedirect()
    {
        if (Membership.ValidateUser(ctrlMainLogin.UserName, ctrlMainLogin.Password))
        {
            MembershipUser currentUser = Membership.GetUser(ctrlMainLogin.UserName);
            Guid userId = (Guid)currentUser.ProviderUserKey;

            //if user is not in cache 
            //- use the standard procedure of checking for filled personnel names
            if (Cache[ctrlMainLogin.UserName] == null)
            {
                //if the user is not in personnel role and is administrator bypass this check
                if (IsInPersonnnelRole(ctrlMainLogin.UserName) && Roles.IsUserInRole(ctrlMainLogin.UserName, ClinicUserRoles.Administrators.ToString()))
                {
                    bool hasUserInfo = CheckDependingOnRole(currentUser.UserName, userId);
                    if (!hasUserInfo)
                    {
                        FormsAuthentication.SignOut();
                        Response.Redirect("ProfileInfo.aspx?UserName=" + ctrlMainLogin.UserName);
                    }
                }
            }
        }
    }

    private bool IsInPersonnnelRole(string userName)
    {
        string[] userRoles = Roles.GetRolesForUser(userName);
        List<string> userRolesList = new List<string>(userRoles);
        bool isInPersonnelRole = false;

        if (userRolesList.Contains(ClinicUserRoles.Attendants.ToString()))
        {
            isInPersonnelRole = true;
        }
        else if (userRolesList.Contains(ClinicUserRoles.Doctors.ToString()))
        {
            isInPersonnelRole = true;
        }
        else if (userRolesList.Contains(ClinicUserRoles.Nurses.ToString()))
        {
            isInPersonnelRole = true;
        }

        return isInPersonnelRole;
    }

    private bool CheckDependingOnRole(string userName, Guid userId)
    {
        //getting information for the user depending of his/her role
        string firstName = string.Empty;
        string middleName = string.Empty;
        string family = string.Empty;

        if (Roles.IsUserInRole(userName, ClinicUserRoles.Doctors.ToString()))
        {
            DoctorsTableAdapter doctorsTA = new DoctorsTableAdapter();
            DoctorsDS.DoctorsDataTable doctorTable = doctorsTA.GetDoctorByUserId(userId);
            if (doctorTable.Rows.Count == 1)
            {
                DoctorsDS.DoctorsRow userRow = (DoctorsDS.DoctorsRow)doctorTable.Rows[0];
                firstName = userRow.FirstName;
                middleName = userRow.MiddleName;
                family = userRow.LastName;
            }
        }
        else if (Roles.IsUserInRole(userName, ClinicUserRoles.Nurses.ToString()))
        {
            NursesTableAdapter NursesTA = new NursesTableAdapter();
            NursesDS.NursesDataTable NurseTable = NursesTA.GetNurseByUserId(userId);
            if (NurseTable.Rows.Count == 1)
            {
                NursesDS.NursesRow userRow = (NursesDS.NursesRow)NurseTable.Rows[0];
                firstName = userRow.FirstName;
                middleName = userRow.MiddleName;
                family = userRow.LastName;
            }
        }
        else if (Roles.IsUserInRole(userName, ClinicUserRoles.Attendants.ToString()))
        {
            AttendantsTableAdapter AttendantsTA = new AttendantsTableAdapter();
            AttendantsDS.AttendantsDataTable AttendantTable = AttendantsTA.GetAttendantByUserId(userId);
            if (AttendantTable.Rows.Count == 1)
            {
                AttendantsDS.AttendantsRow userRow = (AttendantsDS.AttendantsRow)AttendantTable.Rows[0];
                firstName = userRow.FirstName;
                middleName = userRow.MiddleName;
                family = userRow.LastName;
            }
        }

        //check if user didn't fill personal information fields
        bool hasUserInfo = true;
        if (firstName == string.Empty &&
            middleName == string.Empty &&
            family == string.Empty)
        {
            hasUserInfo = false;
        }
        else
        {
            Cache[userName] = true;

        }

        return hasUserInfo;
    }

    #endregion
}
